package com.zhaosc.elasticsearch;

import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.tasks.TaskSubmissionResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.BulkByScrollTask;
import org.elasticsearch.index.reindex.ReindexRequest;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import com.google.common.collect.Maps;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-reindex.html
 * 
 * @author zhaosc
 *
 */
public class ReIndexTest extends BaseTest {

	private static final Logger logger = Logger.getLogger(ReIndexTest.class);

	@Test
	public void testIndex01() {
		Map<String, String> source = Maps.newHashMap();
		source.put("gender", "man");
		source.put("age", "16");
		source.put("xingming", "zhaosc03");
		IndexRequest indexRequest = new IndexRequest("test03");

		indexRequest.source(source);
		RequestOptions options = RequestOptions.DEFAULT;

		try {
			IndexResponse indexResponse = client.index(indexRequest, options);
			Result result = indexResponse.getResult();
			if (Result.CREATED == result) {
				logger.info("****success****");
			} else {
				logger.info("****fail****");
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Test
	public void testReIndex01() {
		ReindexRequest request = new ReindexRequest();
		request.setSourceIndices("test01", "test02", "test03");
		request.setDestOpType("create");
		request.setConflicts("proceed");
//		request.addSortField("xingming", SortOrder.DESC);
//		request.addSortField("age", SortOrder.ASC);
		request.setDestIndex("test04");
		try {
			BulkByScrollResponse response = client.reindex(request, RequestOptions.DEFAULT);

			logger.info(response);
		} catch (IOException e) {
			logger.error("", e);
		}

	}

	@Test
	public void testReIndex02() {
		ReindexRequest request = new ReindexRequest();
		request.setSourceIndices("test01", "test02", "test03");
		//request.setDestOpType("create");
		request.setConflicts("proceed");
		request.addSortField("xingming", SortOrder.DESC);
		request.addSortField("age", SortOrder.ASC);
		request.setDestIndex("test04");
		try {
			TaskSubmissionResponse response = client.submitReindexTask(request, RequestOptions.DEFAULT);
			logger.info(response);
		} catch (IOException e) {
			logger.error("", e);
		}

	}
	
	@Test
	public void testDelete01() {
		DeleteRequest deleteRequest = new DeleteRequest("test01", "mLI8-W4BQXXqneMHvnQS");

		deleteRequest.timeout(TimeValue.timeValueMinutes(2));
		deleteRequest.timeout("2m");
		deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

		try {
			DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
			
			if(DocWriteResponse.Result.DELETED==deleteResponse.getResult()) {
				logger.info("***success***");
			}else{
				logger.info("***fail***");
			}
			
		} catch (IOException e) {
			logger.error(e);
		}
	}

}
